<template>
  <div class="content-container">
    <h1>配置模板字段</h1>
    <!--模板字段列表-->
    <el-card>
      <div slot="header">
        <el-button type="primary" @click="configFieldDialogVisible=true">新增</el-button>
        <el-button @click="goBack">返回模板页</el-button>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column prop="id" label="ID" width="80">
        </el-table-column>
        <el-table-column prop="chTitle" label="字段中文名" width="100">
        </el-table-column>
        <el-table-column prop="fieldName" label="字段标识"> </el-table-column>
        <el-table-column prop="fieldType" label="字段类型"> </el-table-column>
        <el-table-column prop="isCondition" label="是否是筛选条件"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="updateTemplateField(scope.row)">更新</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!--配置字段弹出框-->
    <el-dialog title="新增/更新模板字段" :visible.sync="configFieldDialogVisible">
      <el-form :model="templateFieldForm">
        <el-form-item label="字段中文名" label-width="100px">
          <el-input v-model="templateFieldForm.chTitle"></el-input>
        </el-form-item>
        <el-form-item label="字段标识" label-width="100px">
          <el-input v-model="templateFieldForm.fieldName"></el-input>
        </el-form-item>
        <el-form-item label="字段类型" label-width="100px">
          <el-input v-model="templateFieldForm.fieldType"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="configFieldDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="saveTemplateField">确定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      templateId: 0,
      condition: {
        templateId: 0
      },
      tableData: [],
      configFieldDialogVisible: false,
      templateFieldForm: {
        id: 0,
        templateId: 0,
        chTitle: "",
        fieldName: "",
        fieldType: 1
      }
    }
  },
  
  mounted() {
    // console.log("reoute:::", this.$route);
    this.templateId = this.$route.query.templateId;
    this.templateFieldForm.templateId = this.templateId;
    this.condition.templateId = this.templateId;
    this.requestTemplateFieldList();
  },

  methods: {
    // 请求模板字段列表
    requestTemplateFieldList() {
      this.$axios
        .post("/api/statistics/template-field/list", this.condition)
        .then(res => {
          // console.log(res);
          if (res.data.code === 200) {
            this.tableData = res.data.data;
          }
        })
        .catch(err => console.log(err));
    },

    // 保存模板字段
    saveTemplateField() {
      this.$axios
        .post("/api/statistics/template-field/save", this.templateFieldForm)
        .then(res => {
          // console.log(res);
          if (res.data.code === 200) {
            this.configFieldDialogVisible = false;
            this.requestTemplateFieldList();
            this.resetTemplateFieldForm();
            this.$message.success(res.data.message);
          } else {
            this.$message.error(res.data.message);
          }
        })
        .catch(err => console.log(err))
    },

    // 更新模板字段
    updateTemplateField(field) {
      this.templateFieldForm = field;
      this.configFieldDialogVisible = true;
    },

    // 重置模板字段表单
    resetTemplateFieldForm() {
      this.templateFieldForm = {
        id: 0,
        templateId: this.templateId,
        chTitle: "",
        fieldName: "",
        fieldType: 1
      }
    },

    // 返回
    goBack() {
      this.$router.go(-1);
    }
  }
}
</script>

<style scoped>

</style>